package edcom.client;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

public class ImageWindow extends DialogBox {
	
	private Button imageWindowCloseButton = new Button("Close");
	private Button imageWindowViewButton = new Button("Picture only");
	private Image imageWindowImage = new Image();
	private Label imageWindowImageTitle = new Label();
	private HorizontalPanel dialogHPanel = new HorizontalPanel();
	private VerticalPanel dialogVPanel = new VerticalPanel();
	private String url = null;
	
	public ImageWindow() {
		super(false, true);
		
		//setHTML("<div class='gwt-ImageWindowTitle'>&nbsp;ImageViewer</div>");
		setGlassEnabled(true);
		//setGlassStyleName(getGlassStyleName());
		setStyleName("gwt-ImageWindow");
		setAnimationEnabled(true);
		//setModal(true);
		
		imageWindowCloseButton.getElement().setId("closeButton");
		imageWindowCloseButton.setStyleName("gwt-ImageWindowButton");
		imageWindowCloseButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		
		imageWindowViewButton.getElement().setId("viewButton");
		imageWindowViewButton.setStyleName("gwt-ImageWindowButton");
		imageWindowViewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Window.Location.assign(url);
			}
		});
		
		imageWindowImageTitle.setStyleName("gwt-ImageWindowImageTitle");
//		titleBar.setWidget(0, 1, imageWindowImageTitle);
//		titleBar.setWidget(0, 0, imageWindowCloseButton);
		dialogHPanel.add(imageWindowCloseButton);
		dialogHPanel.add(imageWindowViewButton);
		dialogHPanel.add(imageWindowImageTitle);
		dialogVPanel.add(dialogHPanel);
		dialogVPanel.add(imageWindowImage);
		setWidget(dialogVPanel);
		
	}
	
	void show(String url, String title, String description) {
		
		imageWindowImage.setUrl(this.url = url);
		imageWindowImageTitle.setText("\"" + title + "\" - " + description);
		
		int height = imageWindowImage.getHeight();
		int width = imageWindowImage.getWidth();
		
		int maxheight = Window.getClientHeight() - 60;
		int maxwidth = Window.getClientWidth() - 20;
		
		if((height != maxheight) || (width != maxwidth)) {
			
			int newHeight = (height * maxwidth) / width;
			if (newHeight > maxheight) {
				imageWindowImage.setHeight(maxheight + "px");
			} 
			else {
				imageWindowImage.setWidth(maxwidth + "px");
			}
			
		}
		
		/*
		if(imageWindowImage.getWidth() > Window.getClientWidth() - 20) {
			imageWindowImage.setWidth((Window.getClientWidth() - 20) + "px"); 
			if(imageWindowImage.getHeight() > Window.getClientHeight() - 60) {
				imageWindowImage.setHeight(() + "px");
			}
		}
		else {
			imageWindowImage.setHeight((Window.getClientHeight() - 60) + "px");
		} // */
		
		
		center(); // show in center
	}

}
